package com.gamebox.view;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.gamebox.dao.GameDao;
import com.gamebox.javaBean.User;

public class ChangeInfoDialog extends JDialog{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2845706587252420927L;
	private User user;
	private GameDao gameDao = new GameDao();
	public ChangeInfoDialog(final User user){
		this.user = user;
		this.setTitle("修改个人信息");
		this.setBounds(300, 300, 400, 400);
		Container con=this.getContentPane(); 
		con.setLayout(null);
		JLabel nameLable = new JLabel("用户名:");
		JLabel name = new JLabel(user.getName());
		nameLable.setBounds(20, 20, 100, 30);
		name.setBounds(130, 20, 100, 30);
		JLabel nickLable = new JLabel("昵称:");
		final JTextField nick = new JTextField();
		nickLable.setBounds(20, 60, 100, 30);
		nick.setBounds(130, 60, 100, 30);
		JLabel regLable = new JLabel("注册时间:");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		JLabel regTime = new JLabel(sdf.format(user.getRegisterTime()));
		regLable.setBounds(20, 100, 100, 30);
		regTime.setBounds(130, 100, 100, 30);
		JLabel pass1Lable = new JLabel("密码:");
		final JPasswordField pass1 = new JPasswordField();
		pass1Lable.setBounds(20, 140, 100, 30);
		pass1.setBounds(130, 140, 100, 30);
		JLabel pass2Lable = new JLabel("确认密码:");
		final JPasswordField pass2 = new JPasswordField();
		pass2Lable.setBounds(20, 180, 100, 30);
		pass2.setBounds(130, 180, 100, 30);
		JButton confirm = new JButton("确定");
		confirm.setBounds(130, 220, 100, 30);
		confirm.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String nickName = nick.getText();
				String pass1String = pass1.getText();
				String pass2String  = pass2.getText();
				if(!pass1String.equals(pass2String)){
					JOptionPane.showConfirmDialog(null, "密码不一致");
					return;
				}
				gameDao.updateInfo(nickName,pass1String,user.getId());
				JOptionPane.showMessageDialog(null, "修改个人信息成功");
			}
		});
		
		con.add(nameLable);
		con.add(name);
		con.add(nickLable);
		con.add(nick);
		con.add(regLable);
		con.add(regTime);
		con.add(pass1Lable);
		con.add(pass1);
		con.add(pass2Lable);
		con.add(pass2);
		con.add(confirm);
		this.setVisible(true);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
}
